<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$this->session('roomid',$_RQ['id']);
	}
	$roomid=$this->session('roomid');
	$room=pdo_get('jy_jd_room',array('id'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
	if(!$room){
		exi('请选择房型！','error');
	}
		$currentyear =$_RQ['year']?:date('Y');
		$currentmonth = $_RQ['month']?:date('m');
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $currentyear + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['currentyear']=$currentyear;
		$return['currentmonth']=$currentmonth;
		$return['months']=$months;
		$return['years']=$years;	
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if(!$year){
			$year=$currentyear;
		}
		if(!$month){
			$month=$currentmonth;
		}
		$return['calendar'] =$return['kongday']= array();		
		$return['room'] = $room;
		$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
		exi($return);
}
if($_RQ['op']=='dates'){
	$roomid=$this->session('roomid');
	$room=pdo_get('jy_jd_room',array('id'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$startime=strtotime($year.'-'.$month);
		$endtime=strtotime($year.'-'.$month.'-'.$day);
		$nowtime=array($startime,$endtime);
		$calendar=pdo_getall('jy_jd_room_price',array('roomid'=>$roomid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']),array('roomid','num','price','onday'),'onday');
		if (empty($calendar)) {
			$calendar = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$calendar[date($year . '-' . $month . '-' . $i)] = array();
				++$i;
			}
		}else{
			$result = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$result[$date] = false;
				if (isset($calendar[$date])) {
					$value = $calendar[$date];
					$result[$date] = array(
							'onday' =>$date,
							'price' => $value['price']?:$room['price'], 
							'num' => $value['num']?:$room['num'], 
					);
				}
				++$i;
			}
			$calendar = $result;
		}
		$d=array();
		foreach($calendar as $k=>$v){
			$d[$k]=date('d', strtotime($k));
		}
		$return['calendar'] = $calendar;
		$return['d'] = $d;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['year']=$year;
		$return['month']=$month;
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		exi($return);
}
if($_RQ['op']=='set'){
	$roomid=$this->session('roomid');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$onday = $postdata['onday'];
		if (empty($roomid)){
			exi('请选择房型！','error');
		}
		if (empty($onday)){
			exi('请选择日期！','error');
		}
		$onday2 = strtotime($onday);
		$year = date('Y', $onday2);
		$month = date('m', $onday2);
		$data=array_fields(array('onday','price','num','roomid','storeid'),$postdata);
		$id=intval($postdata['id']);
		$data['ontime']=strtotime($onday);
		$data['onday']=$onday;
		foreach($postdata['taocan'] as $key=>$tv){
			$data['price']=$postdata['price'.$key];
			$isadd=pdo_get('jy_jd_room_price',array('taocankey'=>$key,'onday'=>$onday,'roomid'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']));
			if($isadd['id']){
				pdo_update('jy_jd_room_price',$data,array('id'=>$isadd['id'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$data['uniacid']=$_SESSION['uniacid'];
				$data['roomid']=$this->session('roomid');
				$data['taocankey']=$key;
				pdo_insert('jy_jd_room_price',$data);
			}
		}
		exi('设置成功','','/price_pai//year='.$year.'&month='.$month);
	}
	$onday=$_RQ['onday'];
	if (empty($onday)){
		exi('请选择日期！'.$onday,'error');
	}
	$item=pdo_get('jy_jd_room_price',array('taocankey'=>0,'onday'=>$onday,'roomid'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']));
	$item['onday']=$onday;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'num','title'=>'数量','help'=>'不填或者为0，表示默认！'),
		)
	);
	$room=pdo_get('jy_jd_room',array('id'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']));
	$taocan=json_decode($room['taocan'],true);
	if(count($taocan)<=0){
		$taocan[]=array('title'=>'标准房');
	}
	foreach($taocan as $key=>$tv){
		$price=pdo_getcolumn('jy_jd_room_price',array('taocankey'=>$key,'onday'=>$onday,'roomid'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']),'price');
		$item['price'.$key]=$price?:'';
		$htmls['edit'][]=array('type'=>'string','name'=>'price'.$key,'title'=>$tv['title'].'价格');
	}
	$item['taocan']=$taocan;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='batch_set'){
	$roomid=$this->session('roomid');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['price']){
			exi('请填写价格！','error');
		}
		$price = $postdata['price'];
		$num = $postdata['num'];
		$year = $postdata['year'];
		$month = $postdata['month'];
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$startime=strtotime($year.'-'.$month);
		$endtime=strtotime($year.'-'.$month.'-'.$day);
		$nowtime=array($startime,$endtime);
		pdo_update('jy_jd_room_price',array('price'=>$price,'num'=>$num),array('roomid'=>$roomid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']));
		$i = 1;
		while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$isadd=pdo_get('jy_jd_room_price',array('onday' =>$date,'roomid'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
				if (!$isadd) {
					$data=array(
						'price'=>$price,
						'num'=>$num,
						'onday'=>$date,
						'ontime'=>strtotime($date),
					);
					$data['uniacid']=$_SESSION['uniacid'];
					$data['roomid']=$this->session('roomid');
					pdo_insert('jy_jd_room_price',$data);
				}
				++$i;
		}
		exi('批量设置成功！','','/price_pai/');
	}
	$item=array();
	$item['year']=$_RQ['year'];
	$item['month']=$_RQ['month'];
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'price','title'=>'价格'),
			array('type'=>'string','name'=>'num','title'=>'数量','help'=>'不填或者为0，表示默认！'),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='delete'){
		$postdata=get_postdata();
		$date = trim($postdata['date']);
		if (empty($date)) {
			exi('参数错误，请选择日期！'.$date,'error');
		}
		pdo_delete('jy_jd_room_price',array('onday'=>$date,'roomid'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']));
		exi('删除成功') ;
}
if($_RQ['op']=='clear'){
		$postdata=get_postdata();
		$year = trim($postdata['year']);
		$month = trim($postdata['month']);
		$onday=$year.'-'.$month;
		pdo_delete('jy_jd_room_price',array('onday like'=>'%'.$onday.'%','roomid'=>$this->session('roomid'),'uniacid'=>$_SESSION['uniacid']));
		exi('清除成功') ;
}
?>
